/**
 * 认证工具类
 * 管理登录状态和权限
 */

// 硬编码白名单（实际项目中应该从后端获取）
const WHITELIST = [
  // { username: 'admin', password: 'admin123' },
  // { username: 'manager', password: 'manager123' },
  { username: '15071520541', password: 'asd123123' },
];

export const auth = {
  // 登录验证
  login: (username: string, password: string): Promise<{ success: boolean; token?: string; message?: string }> => {
    return new Promise((resolve) => {
      // 模拟异步验证
      setTimeout(() => {
        const user = WHITELIST.find(
          (u) => u.username === username && u.password === password
        );

        if (user) {
          // 生成简单的token（实际应该由后端生成JWT）
          const token = btoa(`${username}:${Date.now()}`);
          localStorage.setItem('admin_token', token);
          localStorage.setItem('admin_username', username);
          resolve({ success: true, token });
        } else {
          resolve({ success: false, message: '用户名或密码错误' });
        }
      }, 500);
    });
  },

  // 登出
  logout: () => {
    localStorage.removeItem('admin_token');
    localStorage.removeItem('admin_username');
  },

  // 检查是否已登录
  isAuthenticated: (): boolean => {
    return !!localStorage.getItem('admin_token');
  },

  // 获取当前用户名
  getCurrentUser: (): string | null => {
    return localStorage.getItem('admin_username');
  },

  // 获取token
  getToken: (): string | null => {
    return localStorage.getItem('admin_token');
  },
};

